
import UIKit
import SnapKit
import SwiftEventBus


class CustomRoleCell : UICollectionViewCell{
    var role: Role!
    func bindViewModel( role: Role) {
        self.role = role
        //通过富文本来设置行间距
        let paraph = NSMutableParagraphStyle()
        //将行间距设置为28
        paraph.lineSpacing = 8
        //样式属性集合
        let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 12),
                          NSAttributedString.Key.paragraphStyle: paraph]
        subtitleLabel.attributedText = NSAttributedString(string: role.description, attributes: attributes)
        nameLabel.text = role.title
    }
    
    
    
    
    
    var nameLabel: UILabel = {
        let titelLabel = UILabel()
        titelLabel.font = UIFont.boldSystemFont(ofSize: 17)
        titelLabel.textColor = .white
        titelLabel.text = "学习顾问"
        titelLabel.numberOfLines = 2
        return titelLabel
    }()
    
    var chatLabel: UILabel = {
        let titelLabel = UILabel()
        titelLabel.font = UIFont.boldSystemFont(ofSize: 13)
        titelLabel.textColor = .white
        titelLabel.text = "开始对话"
        return titelLabel
    }()
    
    var bottomView: UIView = {
        let bottomView = UIView()
        bottomView.backgroundColor = .primaryColor.withAlphaComponent(0.5)
        bottomView.layer.cornerRadius = 12
        bottomView.layer.maskedCorners = CACornerMask(rawValue: CACornerMask.layerMinXMaxYCorner.rawValue | CACornerMask.layerMaxXMaxYCorner.rawValue)

        return bottomView
    }()
    
    var subtitleLabel: UILabel = {
        let titelLabel = UILabel()
        titelLabel.font = UIFont.systemFont(ofSize: 12)
        titelLabel.textColor = .white
        titelLabel.numberOfLines = 2
        
        
        return titelLabel
    }()
    
    
    
    var chatButton: UIButton = {
        let rightButton = UIButton(type: .custom)
        rightButton.setImage(UIImage(systemName: "trash"), for: .normal)
        rightButton.imageView?.tintColor = .primaryColor.withAlphaComponent(0.5)
        rightButton.addTarget(self, action: #selector(handleRightBarButtonTapped(_:)), for: .touchUpInside)
        
        return rightButton
    }()
    
    
    
    
    @objc private func handleRightBarButtonTapped(_ sender: Any) {
        
        
        SwiftEventBus.post(EVENT_NAME_DELETE_ROLE, sender: role)
        print("handleRightBarButtonTapped")
    }
    
    @objc func chatTapped(tapGestureRecognizer: UITapGestureRecognizer)
    {
        //        delegate?.didTapChat(user: user)
    }
    override init(frame: CGRect) {
        super.init(frame: .zero)
        self.addSubview(nameLabel)
        self.addSubview(subtitleLabel)
        
        self.addSubview(chatButton)
        self.addSubview(bottomView)
        bottomView.addSubview(chatLabel)
        
        self.backgroundColor = primaryBackgroundColor
        self.layer.cornerRadius = 12
        
       
        
       

        chatButton.snp.makeConstraints { make in
            make.top.equalTo(self).offset(8)
            make.right.equalTo(self).offset(-8)
            make.width.equalTo(16)
            make.height.equalTo(16)
        }
        
        
        
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(chatButton)
            make.left.equalTo(self).offset(12)
            make.right.equalTo(chatButton.snp.left).offset(-2)
            
        }
        
        
        subtitleLabel.snp.makeConstraints { make in
            make.top.equalTo(nameLabel.snp.bottom).offset(18)
            make.left.equalTo(nameLabel)
            make.right.equalTo(chatButton)
            
        }
        
        bottomView.snp.makeConstraints { make in
            make.bottom.equalTo(self)
            make.left.equalTo(self)
            make.width.equalTo(self)
            make.height.equalTo(36)
            
        }
        chatLabel.snp.makeConstraints { make in
            make.center.equalTo(bottomView)
            
        }
        
       
    }
    
  
  
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

